package model

import (
	"gitee.com/theegg/go-brick/app/service/app"
	"gitee.com/theegg/go-brick/app/service/help/credis"
	"github.com/gin-gonic/gin"
	"github.com/xormplus/xorm"
	"strconv"
)

// 处理结构体
type AdminQueryParams struct {
	Limit  int
	Offset int
	Params map[string]interface{}
}

// 默认查询条数
var defaultAdminPageSize = 15

// 统一处理 admin 查询参数
func BuildAdminQueryParam(queryParams map[string]interface{}) (*AdminQueryParams, error) {
	returnData := AdminQueryParams{
		Limit:  0,
		Offset: 0,
		Params: make(map[string]interface{}),
	}

	// 处理每页数据量
	var pageSize int
	var errSize error

	if _, ok := queryParams["page_size"]; ok && queryParams["page_size"] != nil {
		pageSize, errSize = strconv.Atoi(queryParams["page_size"].(string))
		if errSize != nil {
			pageSize = defaultAdminPageSize
		}
	} else {
		pageSize = defaultAdminPageSize
	}

	// 偏移数据
	var offset int = 0
	if _, ok := queryParams["page_index"]; ok {
		pageIndex, err := strconv.Atoi(queryParams["page_index"].(string))
		if err != nil {
			return nil, err
		}

		offset = (pageIndex - 1) * pageSize
	}

	returnData.Offset = offset
	returnData.Limit = pageSize
	returnData.Params = queryParams

	return &returnData, nil
}

// 数据库 上下文
type DbContext struct {
	Platform      string
	Sign          string
	Region        string
	ModelName     string
	FromModelName string
}

// @title NewDefaultDbContext
// @description 构建一个默认 context, 多用于商户确认之前
func NewDefaultDbContext() *DbContext {
	return &DbContext{Platform: "admin", Sign: "default", Region: "default", ModelName: "default"}
}

// @title NewDbContext
// @description 获取 context 从 partner
func NewDbContext(partnerSign string, dbRegion string) *DbContext {
	return &DbContext{Platform: "partner", Sign: partnerSign, Region: dbRegion, ModelName: "default"}
}

// @title NewAdminDbContext
// @description 从 Gin Context 绑定 用来链接 admin
func NewAdminDbContext(c *gin.Context, modelName string) *DbContext {
	sign, okSign := c.Get("c_admin_db_sign")
	if !okSign || sign.(string) == "" {
		panic("无效的上下文, 登陆标识")
	}

	region, okRegion := c.Get("c_admin_db_region")
	if !okRegion || region.(string) == "" {
		panic("无效的上下文, 登陆区域")
	}

	return &DbContext{Platform: "admin", Sign: sign.(string), Region: region.(string), ModelName: modelName}
}

// @title NewPartnerDbContext
// @description 从 Gin Context 绑定 用来链接 partner
func NewPartnerDbContext(c *gin.Context, modelName string) *DbContext {
	sign, okSign := c.Get("c_partner_db_sign")
	if !okSign || sign.(string) == "" {
		panic("无效的上下文, 登陆标识")
	}

	region, okRegion := c.Get("c_partner_db_region")
	if !okRegion || region.(string) == "" {
		panic("无效的上下文, 登陆区域")
	}

	return &DbContext{Platform: "partner", Sign: sign.(string), Region: region.(string), ModelName: modelName}
}

// @title NewPartnerDbContext
// @description 从 Gin Context 绑定 用来链接 partner
func NewWwwDbContext(c *gin.Context, modelName string) *DbContext {
	sign, okSign := c.Get("c_partner_db_sign")
	if !okSign || sign.(string) == "" {
		panic("无效的上下文, 登陆标识")
	}

	region, okRegion := c.Get("c_partner_db_region")
	if !okRegion || region.(string) == "" {
		panic("无效的上下文, 登陆区域")
	}

	return &DbContext{Platform: "www", Sign: sign.(string), Region: region.(string), ModelName: modelName}
}

// 从 其他 DbContext Context 绑定
func NewAdminDbContextFromOther(otherDc *DbContext, modelName string) *DbContext {
	if otherDc.Sign == "" || otherDc.Region == "" {
		panic("无效的 - Db上下文环境-" + otherDc.ModelName)
	}

	return &DbContext{Platform: "admin", Sign: otherDc.Sign, Region: otherDc.Region, ModelName: modelName, FromModelName: otherDc.ModelName}
}

// 获取db session
func (dc DbContext) GetDbSession(options ...string) *xorm.Engine {
	if dc.Sign == "" || dc.Region == "" {
		panic("无效的Db上下文环境")
	}

	conn := "master"
	if len(options) >= 1 && options[0] != "" {
		conn = options[0]
	}

	return app.Db(dc.Sign, dc.Region, conn)
}

// 获取redis链接
// 默认是缓存
func (dc DbContext) GetRedisConn() *credis.CRedis {
	return credis.New()
}
